package com.code.leetcode.before50;

class The8 {

    int stats = 0;
    int sign = 1;
    int[][] tables = {{0,1,2,3},{3, 3, 2, 3},{3, 3, 2, 3},{3, 3, 3, 3}};

    long rtn = 0;

    public int get(char c) {
        if (c == ' ') return 0;
        if (c == '+' || c == '-') return 1;
        if (Character.isDigit(c)) return 2;
        return 3;
    }

    public void gets(char c) {
        stats = tables[stats][get(c)];
        if (stats == 2) {
            rtn = rtn * 10 + (c - '0');
            rtn = sign == 1 ? Math.min(rtn, Integer.MAX_VALUE) : Math.min(rtn, -(long)Integer.MIN_VALUE);
        }
        if (stats == 1 && c == '-') sign = -1;
    }

    public int myAtoi(String str) {
        for (int i = 0; i < str.length(); i++) {
            gets(str.charAt(i));
        }
        return sign * (int) rtn;
    }

}